/*
 * Copyright 2017, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ctyon.watchmanager.ui.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.bean.ContactBean;
import com.ctyon.watchmanager.databinding.ItemContactImportBinding;
import com.ctyon.watchmanager.listener.ItemClickListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

public class ContactsImportAdapter extends RecyclerView.Adapter<ContactsImportAdapter.ContactViewHolder> {

    List<ContactBean> mContactBeanList = new ArrayList<>();

    @Nullable
    private final ItemClickListener chatsItemClickListener;

    public ContactsImportAdapter(@Nullable ItemClickListener itemClickListener) {
        chatsItemClickListener = itemClickListener;
        setHasStableIds(true);
    }

    public void setData(final List<ContactBean> contacts) {
        if (mContactBeanList.size() > 0){
            mContactBeanList.clear();
        }
        mContactBeanList.addAll(contacts);
        notifyDataSetChanged();
    }

    public boolean setChecked(int position){
        boolean b = !mContactBeanList.get(position).getChecked();
        mContactBeanList.get(position).setChecked(b);
        notifyItemChanged(position);
        return b;
    }

    @Override
    public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("Lee_ContactAdapter", "onCreateViewHolder");
        ItemContactImportBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.item_contact_import, parent, false);
        binding.setListener(chatsItemClickListener);
        return new ContactViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(ContactViewHolder holder, int position) {
        Log.d("Lee_ContactAdapter", "onBindViewHolder");
        holder.binding.setContact(mContactBeanList.get(position));
        holder.binding.setIndex(position);
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        Log.d("Lee_ContactAdapter", "getItemCount");
        return mContactBeanList == null ? 0 : mContactBeanList.size();
    }

    @Override
    public long getItemId(int position) {
        Log.d("Lee_ContactAdapter", "getItemId position = " + position);
        return position;
    }

    static class ContactViewHolder extends RecyclerView.ViewHolder {

        final ItemContactImportBinding binding;

        public ContactViewHolder(ItemContactImportBinding binding) {
            super(binding.getRoot());
            Log.d("Lee_ContactAdapter", "ChatsViewHolder");
            this.binding = binding;
        }
    }

}
